package com.rojored.layouts.box2DLayoutClasses
{

import Box2DAS.Dynamics.Joints.b2Joint;
import Box2DAS.Dynamics.b2Body;
import Box2DAS.Dynamics.b2World;

/**
 *  Provides groundwork functionality for IPhyicsCorrelate elements.
 */
public class PhysicsCorrelateBase implements IPhyicsCorrelate
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function PhysicsCorrelateBase()
    {
        super();
    }

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    /**
     *  Correlate box2d bodies.
     */
    protected var bodies:Vector.<b2Body> = new Vector.<b2Body>();

    /**
     *  Correlate box2d joints
     */
    protected var joints:Vector.<b2Joint> = new Vector.<b2Joint>();

    //--------------------------------------------------------------------------
    //
    //  Properties: IPhyicsCorrelate
    //
    //--------------------------------------------------------------------------

    //--------------------------------------
    //  centerX
    //--------------------------------------

    /**
     *  @private
     *  Storage for the centerX property
     */
    protected var _centerX:Number;

    /**
     *  @copy IPhyicsCorrelate#centerX
     */
    public function get centerX():Number
    {
        return _centerX;
    }

    /**
     *  @private
     */
    public function set centerX(value:Number):void
    {
        if (_centerX == value)
            return;

        _centerX = value;
    }

    //--------------------------------------
    //  centerY
    //--------------------------------------

    /**
     *  @private
     *  Storage for the centerY property
     */
    protected var _centerY:Number;

    /**
     *  @copy IPhyicsCorrelate#centerY
     */
    public function get centerY():Number
    {
        return _centerY;
    }

    /**
     *  @private
     */
    public function set centerY(value:Number):void
    {
        if (_centerY == value)
            return;

        _centerY = value;
    }

    //--------------------------------------
    //  angle
    //--------------------------------------

    /**
     *  @private
     *  Storage for the angle property
     */
    protected var _angle:Number;

    /**
     *  @copy IPhyicsCorrelate#angle
     */
    public function get angle():Number
    {
        return _angle;
    }

    /**
     *  @private
     */
    public function set angle(value:Number):void
    {
        if (_angle == value)
            return;

        _angle = value;
    }

    //--------------------------------------
    //  width
    //--------------------------------------

    /**
     *  @private
     *  Storage for the width property
     */
    protected var _width:Number;

    /**
     *  @copy  IPhyicsCorrelate#width
     */
    public function get width():Number
    {
        return _width;
    }

    //--------------------------------------
    //  height
    //--------------------------------------

    /**
     *  @private
     *  Storage for the height property
     */
    protected var _height:Number;

    /**
     *  @copy IPhyicsCorrelate#height
     */
    public function get height():Number
    {
        return _height;
    }

    //--------------------------------------------------------------------------
    //
    //  Methods: IPhyicsCorrelate
    //
    //--------------------------------------------------------------------------

    /**
     *   @copy IPhyicsCorrelate#initialize()
     */
    public function initialize(world:b2World, groupIndex:int, x:Number,
                               y:Number, angle:Number, width:Number,
                               height:Number):void
    {
        throw new Error("Abstract function called.");
    }

    /**
     *  @copy IPhyicsCorrelate#setSize()
     */
    public function setSize(width:Number, height:Number):void
    {
        _width = width;
        _height = height;
    }

    /**
     *  @copy IPhyicsCorrelate#setPosition()
     */
    public function setPosition(centerX:Number, centerY:Number,
                                angle:Number):void
    {
        _centerX = centerX;
        _centerY = centerY;
    }

    /**
     *  @copy IPhyicsCorrelate#destroy()
     */
    public function destroy():void
    {
        for each (var joint:b2Joint in joints)
            joint.destroy();

        joints = new Vector.<b2Joint>();

        for each (var body:b2Body in bodies)
            body.destroy();

        bodies = new Vector.<b2Body>();
    }
}
}
